home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / toolkits / libtk / getset.c.z / getset.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  8.8 KB  |  323 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "tk.h"
  5. #include "private.h"
  6.  
  7. /******************************************************************************/
  8.  
  9. int tkGetColorMapSize(void)
  10. {
  11.  
  12.     if (!xDisplay) {
  13.     return 0;
  14.     } else {
  15.     return w.vInfoMain->colormap_size;
  16.     }
  17. }
  18.  
  19. /******************************************************************************/
  20.  
  21. void tkGetMouseLoc(int *x, int *y)
  22. {
  23.     int junk;
  24.  
  25.     *x = 0;
  26.     *y = 0;
  27.     XQueryPointer(xDisplay, w.wMain, (Window *)&junk, (Window *)&junk,
  28.           &junk, &junk, x, y, (unsigned int *)&junk);
  29. }
  30.  
  31. /******************************************************************************/
  32.  
  33. void tkGetSystem(GLenum type, void *ptr)
  34. {
  35.  
  36.     switch (type) {
  37.       case TK_X_DISPLAY:
  38.         if (!xDisplay) tkInitDisplay();
  39.     *(Display **)ptr = xDisplay;
  40.     break;
  41.       case TK_X_WINDOW:
  42.     *(Window *)ptr = w.wMain;
  43.     break;
  44.       case TK_X_SCREEN:
  45.     *(int *)ptr = xScreen;
  46.     break;
  47.       case TK_CONTEXT:
  48.         *(GLXContext *)ptr = w.cMain;
  49.     break;
  50.       default:
  51.     fprintf(stderr, "error tkGetSystem, unknown type = %d\n", type);
  52.     break;
  53.     }
  54. }
  55.  
  56. /******************************************************************************/
  57.  
  58. GLenum tkGetDisplayModePolicy(void)
  59. {
  60.  
  61.     return w.dmPolicy;
  62. }
  63.  
  64.  
  65. /******************************************************************************/
  66.  
  67. GLint tkGetDisplayModeID(void)
  68. {
  69.  
  70.     if ( w.vInfoMain )
  71.         return w.vInfoMain->visualid;
  72.     else 
  73.         return 0;
  74. }
  75.  
  76.  
  77. /******************************************************************************/
  78.  
  79. GLenum tkGetDisplayMode(void)
  80. {
  81.  
  82.     return w.type;
  83. }
  84.  
  85.  
  86. /******************************************************************************/
  87.  
  88. void tkSetFogRamp(int density, int startIndex)
  89. {
  90.     XColor c[256];
  91.     int rShift, gShift, bShift, intensity, fogValues, colorValues;
  92.     int i, j, k;
  93.  
  94.     switch (w.vInfoMain->class) {
  95.       case DirectColor:
  96.     fogValues = 1 << density;
  97.     colorValues = 1 << startIndex;
  98.     for (i = 0; i < colorValues; i++) {
  99.         for (j = 0; j < fogValues; j++) {
  100.         k = i * fogValues + j;
  101.         intensity = i * fogValues + j * colorValues;
  102.         if (intensity > w.vInfoMain->colormap_size) {
  103.             intensity = w.vInfoMain->colormap_size;
  104.         }
  105.         intensity = (intensity << 8) | intensity;
  106.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  107.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  108.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  109.         c[k].pixel = ((k << rShift) & w.vInfoMain->red_mask) |
  110.                  ((k << gShift) & w.vInfoMain->green_mask) |
  111.                  ((k << bShift) & w.vInfoMain->blue_mask);
  112.         c[k].red = (unsigned short)intensity;
  113.         c[k].green = (unsigned short)intensity;
  114.         c[k].blue = (unsigned short)intensity;
  115.         c[k].flags = DoRed | DoGreen | DoBlue;
  116.         }
  117.     }
  118.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  119.     break;
  120.       case GrayScale:
  121.       case PseudoColor:
  122.     fogValues = 1 << density;
  123.     colorValues = 1 << startIndex;
  124.     for (i = 0; i < colorValues; i++) {
  125.         for (j = 0; j < fogValues; j++) {
  126.         k = i * fogValues + j;
  127.         intensity = i * fogValues + j * colorValues;
  128.         if (intensity > w.vInfoMain->colormap_size) {
  129.             intensity = w.vInfoMain->colormap_size;
  130.         }
  131.         intensity = (intensity << 8) | intensity;
  132.         c[k].pixel = k;
  133.         c[k].red = (unsigned short)intensity;
  134.         c[k].green = (unsigned short)intensity;
  135.         c[k].blue = (unsigned short)intensity;
  136.         c[k].flags = DoRed | DoGreen | DoBlue;
  137.         }
  138.     }
  139.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  140.     break;
  141.     }
  142.  
  143.     XSync(xDisplay, 0);
  144. }
  145.  
  146. /******************************************************************************/
  147.  
  148. void tkSetGreyRamp(void)
  149. {
  150.     XColor c[256];
  151.     float intensity;
  152.     int rShift, gShift, bShift, i;
  153.  
  154.     switch (w.vInfoMain->class) {
  155.       case DirectColor:
  156.     for (i = 0; i < w.vInfoMain->colormap_size; i++) {
  157.         intensity = (float)i / (float)w.vInfoMain->colormap_size *
  158.             65535.0 + 0.5;
  159.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  160.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  161.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  162.         c[i].pixel = ((i << rShift) & w.vInfoMain->red_mask) |
  163.              ((i << gShift) & w.vInfoMain->green_mask) |
  164.              ((i << bShift) & w.vInfoMain->blue_mask);
  165.         c[i].red = (unsigned short)intensity;
  166.         c[i].green = (unsigned short)intensity;
  167.         c[i].blue = (unsigned short)intensity;
  168.         c[i].flags = DoRed | DoGreen | DoBlue;
  169.     }
  170.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  171.     break;
  172.       case GrayScale:
  173.       case PseudoColor:
  174.     for (i = 0; i < w.vInfoMain->colormap_size; i++) {
  175.         intensity = (float)i / (float)w.vInfoMain->colormap_size *
  176.             65535.0 + 0.5;
  177.         c[i].pixel = i;
  178.         c[i].red = (unsigned short)intensity;
  179.         c[i].green = (unsigned short)intensity;
  180.         c[i].blue = (unsigned short)intensity;
  181.         c[i].flags = DoRed | DoGreen | DoBlue;
  182.     }
  183.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  184.     break;
  185.     }
  186.  
  187.     XSync(xDisplay, 0);
  188. }
  189.  
  190. /******************************************************************************/
  191.  
  192. void tkSetOneColor(int index, float r, float g, float b)
  193. {
  194.     XColor c;
  195.     int rShift, gShift, bShift;
  196.  
  197.     switch (w.vInfoMain->class) {
  198.       case DirectColor:
  199.     rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  200.     gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  201.     bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  202.     c.pixel = ((index << rShift) & w.vInfoMain->red_mask) |
  203.           ((index << gShift) & w.vInfoMain->green_mask) |
  204.           ((index << bShift) & w.vInfoMain->blue_mask);
  205.     c.red = (unsigned short)(r * 65535.0 + 0.5);
  206.     c.green = (unsigned short)(g * 65535.0 + 0.5);
  207.     c.blue = (unsigned short)(b * 65535.0 + 0.5);
  208.     c.flags = DoRed | DoGreen | DoBlue;
  209.     XStoreColor(xDisplay, w.cMapMain, &c);
  210.     break;
  211.       case GrayScale:
  212.       case PseudoColor:
  213.     if (index < w.vInfoMain->colormap_size) {
  214.         c.pixel = index;
  215.         c.red = (unsigned short)(r * 65535.0 + 0.5);
  216.         c.green = (unsigned short)(g * 65535.0 + 0.5);
  217.         c.blue = (unsigned short)(b * 65535.0 + 0.5);
  218.         c.flags = DoRed | DoGreen | DoBlue;
  219.         XStoreColor(xDisplay, w.cMapMain, &c);
  220.     }
  221.     break;
  222.     }
  223.  
  224.     XSync(xDisplay, 0);
  225. }
  226.  
  227. /******************************************************************************/
  228.  
  229. void tkSetOverlayMap(int size, float *rgb)
  230. {
  231.     XColor c;
  232.     unsigned long *buf;
  233.     int max, i;
  234.  
  235.     if (w.vInfoOverlay->class == PseudoColor) {
  236.     max = (size > w.vInfoOverlay->colormap_size) ?
  237.           w.vInfoOverlay->colormap_size : size;
  238.     buf = (unsigned long *)calloc(max, sizeof(unsigned long));
  239.     XAllocColorCells(xDisplay, w.cMapOverlay, True, NULL, 0, buf, max-1);
  240.     for (i = 1; i < max; i++) {
  241.         c.pixel = i;
  242.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  243.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  244.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  245.         c.flags = DoRed | DoGreen | DoBlue;
  246.         XStoreColor(xDisplay, w.cMapOverlay, &c);
  247.     }
  248.     free(buf);
  249.     }
  250.  
  251.     XSync(xDisplay, 0);
  252. }
  253.  
  254. /******************************************************************************/
  255.  
  256. void tkSetRGBMap(int size, float *rgb)
  257. {
  258.     XColor c;
  259.     int rShift, gShift, bShift, max, i;
  260.  
  261.     switch (w.vInfoMain->class) {
  262.       case DirectColor:
  263.     max = (size > w.vInfoMain->colormap_size) ? w.vInfoMain->colormap_size
  264.                           : size;
  265.     for (i = 0; i < max; i++) {
  266.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  267.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  268.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  269.         c.pixel = ((i << rShift) & w.vInfoMain->red_mask) |
  270.               ((i << gShift) & w.vInfoMain->green_mask) |
  271.               ((i << bShift) & w.vInfoMain->blue_mask);
  272.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  273.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  274.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  275.         c.flags = DoRed | DoGreen | DoBlue;
  276.         XStoreColor(xDisplay, w.cMapMain, &c);
  277.     }
  278.     break;
  279.       case GrayScale:
  280.       case PseudoColor:
  281.     max = (size > w.vInfoMain->colormap_size) ? w.vInfoMain->colormap_size
  282.                           : size;
  283.     for (i = 0; i < max; i++) {
  284.         c.pixel = i;
  285.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  286.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  287.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  288.         c.flags = DoRed | DoGreen | DoBlue;
  289.         XStoreColor(xDisplay, w.cMapMain, &c);
  290.     }
  291.     break;
  292.     }
  293.  
  294.     XSync(xDisplay, 0);
  295. }
  296.  
  297. /******************************************************************************/
  298.  
  299. GLenum tkSetWindowLevel(GLenum level)
  300. {
  301.  
  302.     switch (level) {
  303.       case TK_OVERLAY:
  304.     if (TK_HAS_OVERLAY(w.type)) {
  305.         if (!glXMakeCurrent(xDisplay, w.wOverlay, w.cOverlay)) {
  306.         return GL_FALSE;
  307.         }
  308.     } else {
  309.         return GL_FALSE;
  310.     }
  311.     break;
  312.       case TK_RGB:
  313.       case TK_INDEX:
  314.     if (!glXMakeCurrent(xDisplay, w.wMain, w.cMain)) {
  315.         return GL_FALSE;
  316.     }
  317.     break;
  318.     }
  319.     return GL_TRUE;
  320. }
  321.  
  322. /******************************************************************************/
  323.